<?php

namespace App\Admin\Actions\ShopUser;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopUserModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Exception;

class ShopUserPowerAction extends RowAction
{
    public $name = '数据互通';

    public function __construct(int $flag = 0)
    {
        parent::__construct();
        $this->name = ($flag === 2 ? '打开' : '关闭') . $this->name;
    }

    /**
     * handle
     * @param ShopUserModel $model
     * @return Response
     * @throws Exception
     */
    public function handle(ShopUserModel $model): Response
    {
        $model->user_peer_power = (int)$model->user_peer_power === 1 ? 2 : 1;
        $model->save();
        mq('cacheUser', (string)$model->user_id);
        OperateLogLogic::add(Admin::user()->user_id, 'admin.staff.dataShare', ['userId' => $model->user_id, 'userPeerPower' => (int)$model->user_peer_power === 1 ? 2 : 1]);
        return $this->response()->success('操作成功')->refresh();
    }
}
